package io.xccit.train.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.xccit.train.common.context.MemberLoginContext;
import io.xccit.train.common.page.PageDTO;
import io.xccit.train.common.page.PageVO;
import io.xccit.train.common.util.SnowFlakeUtil;
import io.xccit.train.member.mapper.PassengerMapper;
import io.xccit.train.member.service.PassengerService;
import io.xccit.train.model.member.domain.Passenger;
import io.xccit.train.model.member.domain.PassengerExample;
import io.xccit.train.model.member.domain.dto.PassengerDTO;
import io.xccit.train.model.member.domain.dto.PassengerUpdateDTO;
import io.xccit.train.model.member.domain.vo.PassengerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author CH_ywx
 * @version 1.0
 * @create 2024/11/12
 * <p>乘客业务层</p>
 */
@Service
public class PassengerServiceImpl implements PassengerService {


    @Autowired
    private PassengerMapper passengerMapper;

    /**
     * 新增乘客
     *
     * @param passengerDTO 新增乘客参数
     */
    @Override
    public int insert(PassengerDTO passengerDTO) {
        DateTime now = DateTime.now();
        Passenger passenger = BeanUtil.copyProperties(passengerDTO, Passenger.class);
        passenger.setId(SnowFlakeUtil.nextId());
        passenger.setMemberId(MemberLoginContext.getId());
        passenger.setCreateTime(now);
        return passengerMapper.insert(passenger);
    }

    /**
     * 根据id查询乘客
     *
     * @param id 乘客ID
     * @return 乘客信息
     */
    @Override
    public PassengerVO selectById(Long id) {
        Passenger passenger = passengerMapper.selectByPrimaryKey(id);
        if (passenger == null){
            return null;
        }
        PassengerVO passengerVO = BeanUtil.copyProperties(passenger, PassengerVO.class);
        String idCardNum = DesensitizedUtil.idCardNum(passengerVO.getIdCard(), 1, 2);
        passengerVO.setIdCard(idCardNum);
        return passengerVO;
    }

    /**
     * 更新乘客
     *
     * @param passengerUpdateDTO 更新乘客参数
     */
    @Override
    public int update(PassengerUpdateDTO passengerUpdateDTO) {
        DateTime now = DateTime.now();
        Passenger passenger = BeanUtil.copyProperties(passengerUpdateDTO, Passenger.class);
        passenger.setMemberId(MemberLoginContext.getId());
        passenger.setIdCard(null);
        passenger.setUpdateTime(now);
        return passengerMapper.updateByPrimaryKeySelective(passenger);
    }

    /**
     * 批量删除乘客(与删除公用)
     *
     * @param ids
     */
    @Override
    public int deleteAndBatch(Long[] ids) {
        if (ids == null || ids.length == 0){
            return 0;
        }
        return passengerMapper.deleteAndBatch(ids);
    }

    /**
     * 查询乘客总数
     *
     * @return
     */
    @Override
    public Long count() {
        Long memberId = MemberLoginContext.getId();
        PassengerExample passengerExample = new PassengerExample();
        passengerExample.createCriteria().andMemberIdEqualTo(memberId);
        return passengerMapper.countByExample(passengerExample);
    }


    /**
     * 查询乘客列表
     *
     * @return
     */
    @Override
    public PageVO<PassengerVO> list(PageDTO pageDTO) {
        Long memberId = MemberLoginContext.getId();
        PassengerExample passengerExample = new PassengerExample();
        if (memberId != null || ObjectUtil.isNotNull(memberId)){
            passengerExample.createCriteria().andMemberIdEqualTo(memberId);
        }
        PageHelper.startPage(pageDTO.getPage(),pageDTO.getSize());
        List<Passenger> passengers = passengerMapper.selectByExample(passengerExample);
        PageInfo<Passenger> pageInfo = new PageInfo<>(passengers);
        List<PassengerVO> result = BeanUtil.copyToList(passengers, PassengerVO.class);
        result.forEach(passengerVO -> {
            String idCardNum = DesensitizedUtil.idCardNum(passengerVO.getIdCard(), 1, 2);
            passengerVO.setIdCard(idCardNum);
        });
        PageVO<PassengerVO> pageVO = new PageVO<>(pageInfo.getTotal(), pageInfo.getPages(), result);
        return pageVO;
    }
}
